home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
007
/
masmsubs.arc
/
GETENV.ASM
< prev
next >
Wrap
Assembly Source File
|
1987-07-28
|
3KB
|
142 lines
name getenv
title getenv - return value of environment variable
save macro p1,p2,p3,p4,p5,p6,p7,p8,p9
irp y,<p1,p2,p3,p4,p5,p6,p7,p8,p9>
ifnb <y>
push y
endif
endm
restore macro
irp z,<p9,p8,p7,p6,p5,p4,p3,p2,p1>
ifnb <z>
pop z
endif
endm
endm
endm
cseg segment byte public 'code'
assume cs:cseg,ds:cseg,ss:nothing
comment `
Routine to get the value of an environment variable.
Calling stack frame:
<sp+00> -> dword ptr to environment variable name
<sp+04> -> dword ptr to environment variable return buffer
Where:
the environment variable is simply the name used in the SET command,
null terminated.
the environment variable return buffer is formatted as follows:
ev_buffer db ev_buf_length ; Max length of the buffer area
db ev_buf_length - 1 dup(0)
ev_buf_length = $ - ev_buffer - 1
The string associated with the e.v. will be returned in this area,
and it will be null terminated.
If the e.v. name cannot be found, the buffer will be empty.
`
public getenv
ev_name equ [bp+4]
ev_buf equ [bp+8]
getenv proc near
push bp
mov bp,sp ; Get frame pointer
save ax,bx,cx,dx,si,di,ds,es
mov ah,30h ; Get DOS version
int 21h
;
; We're going to assume that if it ain't v3.x, it's v2.x. Who uses v1.x?
; I suppose it could fail with v4.x, but I don't think that's something
; I need to worry about.
;
mov ah,51h ; Assume 2.x
cmp al,3 ; At least 3.0?
jb ge_l1 ; No, assume 2.x
mov ah,62h ; If at least 3.0, use funct 62h
ge_l1:
sub al,al ; Just in case...
int 21h ; Get the PSP seg for our process
mov ds,bx ; Point to the PSP
mov bx,2ch ; Point to env base seg
mov ds,[bx] ; Get that pointer
les di,ev_name ; Point to the name they're looking for
sub si,si ; Start at beginning of env
ge_l3:
push di ; Save this pointer
ge_l4:
cmp byte ptr es:[di],0 ; Is this the terminator?
je ge_l6 ; If so, done looking
mov al,es:[di]
cmp al,'a'
jb ge_l41
cmp al,'z'+1
jae ge_l41
sub al,20h
mov es:[di],al
ge_l41:
cmpsb ; Compare a byte
je ge_l4 ; If not a match, stop looking
ge_l5:
cmp byte ptr [si],0 ; Scan for 0 byte
je ge_l7 ; When found, continue
inc si ; Point to next
jmp short ge_l5
ge_l7:
pop di ; Get pointer back
inc si
cmp byte ptr [si],0 ; Is this the second null?
jne ge_l3 ; If not, then keep looking
les di,ev_buf
sub al,al
inc di
stosb
jmp short ge_done ; If it is, we're done looking
ge_l6:
cmp byte ptr [si],'=' ; Is it the separator?
jne ge_l5 ; If not, this ain't the one...
inc si ; Point past the "="
pop di ; Clear stack
les di,ev_buf ; Point to buffer
mov cl,es:[di] ; Get max buffer len
sub ch,ch
dec cx ; Allow for terminator
inc di ; Point to first byte of buffer
ge_l8:
lodsb ; Get a byte
stosb ; And put it in
or al,al ; Is it the terminator?
je ge_l9
loop ge_l8 ; Do for up to max buffer len
ge_l9:
sub al,al
stosb ; Put in OUR terminator
ge_done:
restore
pop bp ; Restore old frame pointer
ret 8
getenv endp
cseg ends
end